<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>双向绑定</title>
    <script src="js/vue.js"></script>
    <style>
        *{
            margin:0;
            padding: 0;
            list-style: none;
        }
        .error{
            color:red
        }
        .success{
            color:green
        }

    </style>
</head>
<body>
    <div id="app">
        {{username}}
        <div>姓名   <input type="text" v-model="username"> 
            <span :class="usernametipclass">{{usernametip}}</span>
        </div>
    </div>
    
</body>
<script>
    //用了v-model 如何去捕获数据的变化
    new  Vue({
        el:"#app",
        data:{
            username:"",
            usernametip:"请输入用户名",
            usernametipclass:"error",
            list:["leson","lulu","lili"]
        },
        watch:{//观察 监听
            username(newValue,oldValue){
                // console.log(newValue,oldValue);
                // if(this.list.some(function(item){ return  item == newValue})){//存在
                // if(this.list.some((item)=>{return item==newValue})){
                if(!this.list.some(item=>item ==newValue)){
                    this.usernametip = "√";
                    this.usernametipclass = "success";
                }else{
                    this.usernametip = "该用户名已经存在";
                    this.usernametipclass = "error";
                }

            }

        }
    })

</script>
</html>